package http;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class Request {
	private String url;
	private String method;
	private Map<String,String> parameter;
	
	public Request(InputStream in){
		try {
			Thread.sleep(500);
			parameter = new HashMap<>();
			byte[] bytes= new byte[in.available()];
			in.read(bytes);
			String context = new String(bytes);
			//System.out.println(context);
			if(!"".equals(context)){
				String p = context.split("\n")[0];
				String[] split = p.split(" ");
				method = split[0];
				String allUrl = split[1];
				String[] urls = allUrl.split("\\?");
				url = urls[0];
				if(urls.length>1){
					setParameter(urls[1]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void setParameter(String ps) {
		String[] parames = ps.split("&");
		for (int i = 0; i < parames.length; i++) {
			String[] parame = parames[i].split("=");
			parameter.put(parame[0], parame[1]);
		}
	}

	public String getUrl(){
		return url;
	}
	
	public String getMethod(){
		return method;
	}
	
	public Map<String,String> getAllParameter(){
		return parameter;
	}
	
	public String getParameter(String key){
		return parameter.get(key);
	}
}
